/**
 * 字典项数据处理方法
 * @author xiaoxujs
 * @date 2023-11-15
 */



import { message } from 'antd';
import { IDataList, IXqSysDictTypeAllTypeAndData } from '../../../type/commit';
import { getXqSysDictTypeAllTypeAndData } from '../../../api/dropDownApi';

/**
 * 获取所有的字典项然后保存到session里面
 * @ahthor xiaoxujs
 */
export const getAllDictionaryEntries = async (fun?: () => void) => {
    const dataValue = (await getXqSysDictTypeAllTypeAndData()) as {
        code: number;
        msg: string;
        data: IXqSysDictTypeAllTypeAndData[];
    };
    if (dataValue.code === 20000) {
        sessionStorage.setItem("zidianxiang", JSON.stringify(dataValue.data));
        fun && fun()
    } else {
        message.error(dataValue.msg);
    }
}
// 获取字典项中的一类
export const handleGetDictionaryItem = (dictType: string) => {
    const dataValue: IXqSysDictTypeAllTypeAndData[] = JSON.parse(sessionStorage.getItem("zidianxiang") as string);
    console.log(dataValue)
    const dictItem = dataValue.find((item: IXqSysDictTypeAllTypeAndData) => {
        return item.dictType === dictType;
    });
    console.log(dictItem)
    if (dictItem) {
        return dictItem.dataList;
    } else {
        return [];
    }
}
// 获取字典项对应的值Value
export const handleGetDictionaryItemValue = (dictType: string, value?: string) => {

    const dataValue: IXqSysDictTypeAllTypeAndData[] = JSON.parse(sessionStorage.getItem("zidianxiang") as string);
    const dictItem = dataValue.find((item: IXqSysDictTypeAllTypeAndData) => {
        return item.dictType === dictType;
    });
    let str = '无';
    dictItem?.dataList.forEach(element => {
        if (element.value === String(value)) {
            str = element.label;
        }
    });

    return str;
}
// 获取字典项对应的值Value
export const handleGetDictionaryItemData: (dictType: string, value?: string) => IDataList | '无' = (dictType, value) =>  {

    const dataValue: IXqSysDictTypeAllTypeAndData[] = JSON.parse(sessionStorage.getItem("zidianxiang") as string);
    const dictItem = dataValue.find((item: IXqSysDictTypeAllTypeAndData) => {
        return item.dictType === dictType;
    });
    let str: IDataList | '无' = '无';
    dictItem?.dataList.forEach(element => {
        if (element.value === String(value)) {
            str = element;
        }
    });

    return str;
}
// //  获取字典项对用的 颜色
// export const handleGetDictionaryItemColor = (dictType: string, value: string) => {
//     const dataValue: IXqSysDictTypeAllTypeAndData[] = JSON.parse(sessionStorage.getItem("zidianxiang") as string);
//     const dictItem = dataValue.find((item: IXqSysDictTypeAllTypeAndData) => {
//         return item.dictType === dictType;
//     });
//     let str = '#000000';
//     dictItem?.dataList.forEach(element => {
//         if (element.value === String(value)) {
//             str = element.cssClass;
//         }
//     });
//     return str;
// }
// 获取字典项对应的默认值
export const handleDictionaryEntryDefaultValue = (dictType: string) => {
    const dataValue: IXqSysDictTypeAllTypeAndData[] = JSON.parse(sessionStorage.getItem("zidianxiang") as string);
    const dictItem = dataValue.find((item: IXqSysDictTypeAllTypeAndData) => {
        return item.dictType === dictType;
    });
    let showValue = dictItem?.dataList.find((item: IDataList) => {
        return item.isDefault === 'Y';
    });
    return showValue?.value;

}
//  获取字典项对用的 颜色
export const handleGetDictionaryItemColor = (dictType: string, value: string) => {
    const dataValue: IXqSysDictTypeAllTypeAndData[] = JSON.parse(sessionStorage.getItem("zidianxiang") as string);
    const dictItem = dataValue.find((item: IXqSysDictTypeAllTypeAndData) => {
        return item.dictType === dictType;
    });
    let str = '#000000';
    dictItem?.dataList.forEach(element => {
        if (element.value === String(value)) {
            str = element.cssClass;
        }
    });
    return str;
}

